import { defineStore } from "pinia";
import { UserState } from "@/stores/interface";
import piniaPersistConfig from "@/stores/helper/persist";
import { findUserDetailClient, findCompanyClient } from "@/api/modules/user";

export const useUserStore = defineStore({
  id: "geeker-user",
  state: (): UserState => ({
    token: "",
    username: "",
    userInfo: {
      userId: "",
      fullName: "",
      companyNo: "",
      companyName: "",
      roleCode: [],
      loginName: "",
      job: "",
      orgNo: "",
      orgName: ""
    },
    menuFlag: "",
    departmentTree: undefined
  }),
  getters: {},
  actions: {
    // Set Token
    setToken(token: string) {
      this.token = token;
    },
    setUsername(username) {
      this.username = username;
      // 获取当前用户信息
      findUserDetailClient({
        "userDto.loginName": username
      }).then((res: any) => {
        this.userInfo = res;
      });
    },
    setMenuFlag(menuFlagStr: string) {
      this.menuFlag = menuFlagStr;
    },
    // Set setUserInfo
    setUserInfo(userInfo: UserState["userInfo"]) {
      this.userInfo = userInfo;
    },
    setDepartmentTree(tree: any[]) {
      this.departmentTree = tree;
    },
    getDepartmentTree() {
      return this.departmentTree;
    }
  },
  persist: piniaPersistConfig("geeker-user")
});
